package com.java.prttern.notes;

import com.java.prttern.Facotry.Circle;
import com.java.prttern.Facotry.Rectangle;
import com.java.prttern.Facotry.Shape;
import com.java.prttern.Facotry.Square;

import java.lang.reflect.InvocationTargetException;

public class ShapeFactory2 {
    //使用getShape方法获取形状类型的对象

   /* public Shape getShape(String shapeType){
        if (shapeType==null){
            return null;
        }
        if (shapeType.equalsIgnoreCase("CIRCLE")){
            return new Circle();
        }else if (shapeType.equalsIgnoreCase("RECTANGLE")){
            return new Rectangle();
        }else if (shapeType.equalsIgnoreCase("SQUARE")){
            return new Square();
        }
        return null;
    }*/
   //创建上述工厂，这个创建函数，没能看出有什么特别的，还特别繁琐，
    // 如果Ishape类太多，那么if else 语句也将增加太多，不好维护，与其这样写，还不如改成入下：

    //使用getShape方法获取形状类型的对象
    public Shape getShape(Class<?> clazz){
        try {
            return (Shape) clazz.getDeclaredConstructor().newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return null;
    }
}
